﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;


/// <summary>
/// Summary description for Sh
/// </summary>
/// 
namespace Shut
{
	public class Utils
	{
		private static string _RelativeWebRoot;
		public static string RelativeWebRoot
		{
			get
			{
				if (_RelativeWebRoot == null)
					_RelativeWebRoot = VirtualPathUtility.ToAbsolute("~/");

				return _RelativeWebRoot;
			}
		}
		public static Uri AbsoluteWebRoot
		{
			get
			{

				HttpContext context = HttpContext.Current;

				if (context.Items["absoluteurl"] == null)
					context.Items["absoluteurl"] = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority) + RelativeWebRoot);

				return context.Items["absoluteurl"] as Uri;

			}
		}

		public static string RemoveIllegalCharacters(string text)
		{
			if (string.IsNullOrEmpty(text))
				return text;

			text = text.Replace(":", string.Empty);
			text = text.Replace("/", string.Empty);
			text = text.Replace("?", string.Empty);
			text = text.Replace("#", string.Empty);
			text = text.Replace("[", string.Empty);
			text = text.Replace("]", string.Empty);
			text = text.Replace("@", string.Empty);
			text = text.Replace("*", string.Empty);
			text = text.Replace(".", string.Empty);
			text = text.Replace(",", string.Empty);
			text = text.Replace("\"", string.Empty);
			text = text.Replace("&", string.Empty);
			text = text.Replace("'", string.Empty);
			text = text.Replace(" ", "-");
			text = RemoveDiacritics(text);
			text = RemoveExtraHyphen(text);

			return HttpUtility.UrlEncode(text).Replace("%", string.Empty);
		}
		private static String RemoveDiacritics(string text)
		{
			String normalized = text.Normalize(NormalizationForm.FormD);
			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < normalized.Length; i++)
			{
				Char c = normalized[i];
				if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
					sb.Append(c);
			}

			return sb.ToString();
		}
		private static string RemoveExtraHyphen(string text)
		{
			if (text.Contains("--"))
			{
				text = text.Replace("--", "-");
				return RemoveExtraHyphen(text);
			}

			return text;
		}

		public static string ToSentenceCase(string s){
			// change first letter to caps
			return s.Substring(0, 1).ToUpper() + s.Substring(1);
		}
		public static string GetTimeLiteral(DateTime dt)
		{
			var ts = new TimeSpan(DateTime.Now.Ticks - dt.Ticks);
			double delta = ts.TotalSeconds;

			const int SECOND = 1;
			const int MINUTE = 60 * SECOND;
			const int HOUR = 60 * MINUTE;
			const int DAY = 24 * HOUR;
			//const int MONTH = 30 * DAY;

			if (delta < 1 * MINUTE)
			{
				return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
			}
			if (delta < 2 * MINUTE)
			{
				return "a minute ago";
			}
			if (delta < 45 * MINUTE)
			{
				return ts.Minutes + " minutes ago";
			}
			if (delta < 120 * MINUTE)
			{
				return "an hour ago";
			}
			if (delta < 24 * HOUR)
			{
				return ts.Hours + " hours ago";
			}
			if (delta < 48 * HOUR)
			{
				return "yesterday";
			}
			if (delta < 5 * DAY)
			{
				return ts.Days + " days ago";
			}
			//if (delta < 12 * MONTH)
			//{
			//    int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
			//    return months <= 1 ? "one month ago" : months + " months ago";
			//}
			//else
			//{
			//    int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
			//    return years <= 1 ? "one year ago" : years + " years ago";
			//} 
			else
			{
				return dt.ToString("ddd d MMM, hh:mm tt");
			}

		}
		public static string GetTimeLiteral(DateTime start, DateTime end)
		{
			// check if now is within what value of start and end

			var ts = new TimeSpan(DateTime.Now.Ticks - start.Ticks);
			double delta = ts.TotalSeconds;

			var ts_ses = new TimeSpan(end.Ticks - start.Ticks);
			double len = (double)ts_ses.TotalMinutes;

			const int SECOND = 1;
			const int MINUTE = 60 * SECOND;
			const int HOUR = 60 * MINUTE;
			const int DAY = 24 * HOUR;
			//const int MONTH = 30 * DAY;
			if (delta < -2 * HOUR)
			{
				return start.ToString("dddd h:mm tt") + " - " + end.ToString("h:mm tt"); ;
			}
			if (delta < -61 * MINUTE)
			{
				return start.ToString("h:mm tt") + ", in 1 hour, " + Math.Abs(ts.Minutes) + " minutes";
			}
			if (delta < -11 * MINUTE)
			{
				return start.ToString("h:mm tt") + ", in " + Math.Abs(ts.Minutes) + " minutes";
			}
			if (delta < -1 * MINUTE)
			{
				return "In less than 10 minutes";
			}

			if (delta < (len - 10) * MINUTE)
			{
				return "Now On!";
			}
			// thats an assumuption!
			if (delta < len * MINUTE)
			{
				return "ends in " + (len - (double)ts.TotalMinutes) + " minutes";
			}
			if (delta < (len + 30) * MINUTE)
			{
				return "Just ended";
			}
			if (delta < (len + 90) * MINUTE)
			{
				return "Ended about an hour ago";
			}
			else
			{
				return "Ended on " + end.ToString("dddd h:mm tt");
			}
		}

		public static string GetTimeLiteral(DateTime start, double len)
		{

			return GetTimeLiteral(start, start.AddMinutes(len));
		}

		public static string GetVersion(){
			// if debung return a random number, else return configuration version
			int _i = 0;
			if (SiteSettings.Instance.DebugMode){
				Random _r = new Random();
				_i = _r.Next(100);
			}
			return _i.ToString();

		}

        public static string GetResourceValue(string resourceKey)
        {
            return (string)HttpContext.GetGlobalResourceObject("LocalizedText", resourceKey);
        }
	}
}